package com.yunupmall.controller.amdin;

import com.yunupmall.entity.admin.commoditypojo.Adminusers;
import com.yunupmall.service.admin.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class LoginController {
    @Autowired
    LoginService loginService;

    @RequestMapping("/loginUser")
    public String selectLogin(@RequestParam("user") String user, @RequestParam("pass") String pass, HttpSession session) {
        List<Adminusers> userData = loginService.getUserData(user, pass);
        if (userData.size() > 0) {
            session.setAttribute("Users", userData.get(0).getUsername());
            return "ok";
        }
        return "fail";
    }

    @GetMapping("/getUserName")
    public String getUserName(HttpSession session) {
        String attribute = String.valueOf(session.getAttribute("Users"));
        return attribute;
    }
}
